/* eslint-disable */
import { saveAs } from 'file-saver'
import * as XLSX from 'xlsx'

export function exportJsonToExcel(tHeader, data, filename = 'excel-list') {
  // 1. 创建工作簿
  const wb = XLSX.utils.book_new()
  
  // 2. 将数据转换为工作表
  const ws = XLSX.utils.json_to_sheet([])
  
  // 3. 添加表头
  XLSX.utils.sheet_add_aoa(ws, [tHeader], { origin: 'A1' })
  
  // 4. 添加数据
  XLSX.utils.sheet_add_json(ws, data, { header: tHeader, skipHeader: true, origin: 'A2' })
  
  // 5. 将工作表添加到工作簿
  XLSX.utils.book_append_sheet(wb, ws, 'Sheet1')
  
  // 6. 生成Excel文件并下载
  const excelBuffer = XLSX.write(wb, { bookType: 'xlsx', type: 'array' })
  const blob = new Blob([excelBuffer], { type: 'application/octet-stream' })
  saveAs(blob, `${filename}.xlsx`)
}